package fun.codefarmer.pm.controller.management.basic;

import fun.codefarmer.pm.model.RespBean;
import fun.codefarmer.pm.model.Role;
import fun.codefarmer.pm.service.MenuRoleService;
import fun.codefarmer.pm.service.MenuService;
import fun.codefarmer.pm.service.PermissService;
import fun.codefarmer.pm.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @ ClassName PermissController 角色管理，权限（哪类人看哪类菜单）
 * @ Descriotion TODO
 * @ author  codeFarmer_z
 * @ 公众号：码农教程
 * @ Date 2022/4/20 20:32
 **/
@RestController
@RequestMapping("/system/basic/permiss")
public class PermissController {
    @Autowired
    private PermissService permissService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private MenuRoleService menuRoleService;
    @Autowired
    private RoleService roleService;

    @GetMapping("/")
    public RespBean getAllRoleAndMenu() {
        RespBean respBean=permissService.getAllRole();
        return respBean;
    }

    /**
     * 查询角色下所有menu
     * @return
     */
    @GetMapping("/menuall")
    public RespBean getMenuAll() {
        RespBean respMenuAll = menuService.getMenuAll();
        return respMenuAll;
    }

    /**
     * 根据角色id查询角色对应的menu权限
     * @param rid
     * @return
     */
    @GetMapping("/menuidsbyroleid/{rid}")
    public RespBean getMenuIdsByRoleId(@PathVariable Integer rid) {
        RespBean resp = menuRoleService.getMenuIdsByRoleId(rid);
        return resp;
    }

    /**
     * 修改角色中的资源
     * @param rid 角色id
     * @param menuids 资源id 数组
     * @return
     */
    @PutMapping("/update")
    public RespBean update(Integer rid,Integer[] menuids) {
        RespBean resp = menuRoleService.updateMenuIdsByRid(rid, menuids);
        return resp;
    }

    /**
     * 添加角色，
     * @param role
     * @return
     */
    @PostMapping("/")
    public RespBean inserRol(@RequestBody Role role) {
        RespBean isture = menuRoleService.insert(role);
        return isture;
    }

    /**
     * 删除角色，同时删除对应角色的资源
     * @param rid
     * @return
     */
    @DeleteMapping("/{rid}")
    @Transactional
    public RespBean deleteRole(@PathVariable Integer rid) {
        Boolean is = menuRoleService.deleteMenuByRid(rid);
        Boolean isTure = roleService.deleteRoleById(rid);
        if (is && isTure) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

}
